<template>
	<view>
		<view v-if='profile != ""' class='member-card'>
			<u-row @click='getUserInfo'>
				<u-col span='2'>
					<!-- <view v-if='profile.avatarUrl'>
						<u-avatar :src="profile.avatarUrl"></u-avatar>
					</view>
					<view v-else> -->
						<u-avatar src="/static/avatar.png"></u-avatar>
					<!-- </view> -->
				</u-col>
				<u-col span='10'>
					<!-- <view v-if='profile.nickname == "" || profile.nickname == null'>
						[点击设置昵称]
					</view>
					<view v-else>
						{{profile.nickname}}
					</view> -->
					<view style='font-size:14px;'>
						{{maskPhoneNumber(profile.phone)}}
					</view>
				</u-col>
			</u-row>
			<view style='padding:10px 0px;'>
				NO.80015328{{profile.id}}
			</view>

			<view v-if='balance != ""' @click='toUrl("/pages/mine/balance-log")'>
				<text>{{balance.point}}积分</text>
				<text style='font-size:10px;'> 明细>> </text>
			</view>
			<view style='padding-top:10px;font-size:10px;'>
				新会名苑新天地
			</view>
			<view style='font-size:10px;'>
				注册时间：{{$u.timeFormat(new Date(profile.createdTime), 'yyyy-mm-dd')}}
			</view>
		</view>
		<view v-else style='height: 22vh;padding:12vh 10px 0px 10px'>
			<u-button type='success' text='手机快捷登录' size='large' open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber"></u-button>
		</view>
		<view class='cate'>
			<text class='category'>
				<text>常用工具</text>
			</text>
		</view>
		<u-row>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/mine/coupon")'>
					<img src='/static/my-coupon.png' style='width:35px;height:35px'>
					<p class='icon-name'>我的卡券</p>
				</view>
			</u-col>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/mine/message")'>
					<img src='/static/message.png' style='width:35px;height:35px'>
					<p class='icon-name'>我的消息</p>
				</view>
			</u-col>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/mine/draw-log")'>
					<img src='/static/draw-log.png' style='width:35px;height:35px'>
					<p class='icon-name'>抽奖记录</p>
				</view>
			</u-col>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/content/index?id=3")'>
					<img src='/static/member.png' style='width:35px;height:35px'>
					<p class='icon-name'>会员权益</p>
				</view>
			</u-col>
		</u-row>
		<u-row>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/content/index?id=1")'>
					<img src='/static/agreement.png' style='width:35px;height:35px'>
					<p class='icon-name'>用户协议</p>
				</view>
			</u-col>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/mine/self-point")'>
					<img src='/static/get-point.png' style='width:35px;height:35px'>
					<p class='icon-name'>自助积分</p>
				</view>
			</u-col>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/content/index?id=4")'>
					<img src='/static/new-content.png' style='width:35px;height:35px'>
					<p class='icon-name'>最新活动</p>
				</view>
			</u-col>
			<u-col span='3'>
				<view class='icon' @click='toUrl("/pages/mine/feedback")'>
					<img src='/static/feed-back.png' style='width:35px;height:35px'>
					<p class='icon-name'>意见反馈</p>
				</view>
			</u-col>
		</u-row>
		<view v-if='profile != ""' style='padding:30px 10px 0px 10px;'>
			<u-button type='success' size='large' shape='circle' text='退出登录' @click='logout'></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				profile: '',
				balance: ''
			}
		},
		onLoad() {
			let token = uni.getStorageSync('api_token')
			if (token == '') {
				this.wxLogin()
			} else {
				this.getUserProfile()
			}
		},
		onShareAppMessage(res) {
			return {
				title: '名苑新天地',
				path: `/pages/index/index`,
				imageUrl: 'https://picture.jmgc.vip/bd3cb5a2-911a-4620-a038-e334c807be3a.png'
			}
		},
		methods: {
			wxLogin() {
				let _this = this
				uni.login({
					success(result) {
						let code = result.code
						_this.request.get(`wechat/mini/login`, {
							code
						}).then((res) => {
							console.log('login', res)
							const token = res.data.token
							uni.setStorageSync('api_token', token)
							this.getUserProfile()
						})
					}
				})
			},
			logout() {
				// uni.removeStorageSync('api_token')
				this.profile = ''
			},
			toUrl(page) {
				uni.navigateTo({
					url: page
				})
			},
			getUserProfile() {
				this.request.get(`api/user`, {
					'@expands': `['entity.profile','entity.balance']`
				}).then((res) => {
					console.log('user', res.data)
					if (res.data.profile && res.data.profile.__metadata.phone && res.data.profile.__metadata
						.phone != '') {
						this.profile = res.data.profile.__metadata
					}
					if (res.data.balance) {
						this.balance = res.data.balance.__metadata
					}
				})
			},
			getPhoneNumber(e) {
				uni.showLoading()
				let _this = this
				if (typeof(e.detail.iv) == 'undefined') {
					uni.showToast({
						title: '获取失败,请重试',
						icon: 'none'
					})
					uni.hideLoading()
					return
				} else {
					uni.login({
						provider: "weixin",
						success: function(res) {
							let data = {
								code: res.code
							}
							_this.request.get("wechat/mini/login", data).then((res) => {
								uni.setStorageSync('api_token', res.data.token);
								if (res.data.data.session_key) {
									let data = {
										sessionKey: res.data.data.session_key,
										iv: e.detail.iv,
										encryptedData: e.detail.encryptedData,
									}
									_this.request.post(`wechat/decrypt-data`, data).then((res) => {
										this.phone = res.data.phoneNumber
										console.log('phone', res);
										//更新手机号码
										_this.request.put(`api/user-profile`, {
											phone: res.data.phoneNumber
										}).then((res) => {
											uni.hideLoading()
											console.log(res)
											_this.getUserProfile()
										})
									})
								} else {
									uni.hideLoading()
									uni.showToast({
										title: '获取手机号码,请重试！',
										icon: 'none'
									})
								}
							})
						},
					});
				}
			},
			showTips() {
				uni.showToast({
					title: '敬请期待！',
					icon: 'none'
				})
			},
			getUserInfo() {
				uni.getUserProfile({
				    desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				    success: (res) => {
						console.log('用户信息：', res.userInfo);
						this.request.put(`api/user-profile`,{avatarUrl: res.userInfo.avatarUrl})
					}	
				});
			},
			maskPhoneNumber(phoneNumber) {
			  // 假设电话号码是一个字符串，并且长度符合一般电话号码的格式
			  // 例如："12345678901"
			  if (!phoneNumber || phoneNumber.length < 10) {
			    // 如果电话号码无效，则返回原样或进行其他处理
			    return phoneNumber;
			  }
			
			  // 将电话号码分割为前三位、中间四位和后四位
			  const prefix = phoneNumber.substr(0, 3);
			  const suffix = phoneNumber.substr(7);
			  // 中间部分用*代替
			  const maskedMiddle = '*'.repeat(4);
			
			  // 将分割后的部分和替换后的中间部分组合起来
			  return prefix + maskedMiddle + suffix;
			}
		}
	}
</script>

<style>
	.member-card {
		background-image: url('https://picture.jmgc.vip/591830d9-bd6d-492f-a7df-ed09086811b3.jpeg');
		background-repeat: repeat;
		background-size: 100%;
		padding: 20px;
		margin: 10px;
		border-radius: 5px;
		color: #fff;
	}

	.category {
		margin-top: 20px;
		background-color: #002238;
		color: #fff;
		padding: 3px 10px;
		border-radius: 0px 40px 40px 0px;
		width: 20%;
	}


	.icon {
		text-align: center;
		align-items: center;
		padding: 30px 0px 20px;
	}

	.icon-name {
		color: #909399;
		font-size: 14px;
		margin-top: 10px;
	}
</style>